package com.mall.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Maps;
import com.mall.entity.sys.ProdtypeEO;
import com.mall.entity.sys.ProductEO;
import com.mall.service.sys.IProdtypeService;
import com.mall.service.sys.IProductService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Controller
public class IndexController {
	
	@Autowired
	private IProdtypeService prodtypeService ;
	
	@Autowired
	private IProductService productService;
	
	@GetMapping("/login")
//	@RequestMapping(value="/login",method= {RequestMethod.GET,RequestMethod.POST})
	public String login() {
		return "login";
	}
	
	@GetMapping("/failure")
	public String failure(Model m) {
		m.addAttribute("msg", "用户名密码错了!!!!!");
		return "login";
	}
	
	
	@ApiOperation(value="商城首页",notes="显示商城首页基本信息，商品信息")
	@GetMapping({"","/"})
	public String index(Model m ) {
		List<ProdtypeEO> allList = prodtypeService.list();
		List<ProdtypeEO> parentList = new ArrayList<>();
		for(ProdtypeEO eo:allList) {//查找所有的父类型
			if(eo.getParentId() == null) {
				List<ProdtypeEO> childs = new ArrayList<>();
				for(ProdtypeEO eo1:allList) {//查找当前父类型的子类型
					if(eo1.getParentId() == eo.getTypeId()) {
						childs.add(eo1);
					}
				}
				eo.setChilds(childs);
				parentList.add(eo);
			}
		}
		m.addAttribute("prodtypelist", parentList);
		m.addAttribute("newprod",  productService.newProd());
		
		
		List<ProductEO> hdlist = productService.queryHDListByType1();//7月7大甩卖
		List<ProductEO>hdlist2 = productService.queryHDListByType2();//815大甩卖
		m.addAttribute("hdlist", hdlist);
		m.addAttribute("hdpages", hdlist.size()/3+(hdlist.size()%3 == 0?0:1));
		m.addAttribute("hdsize", hdlist.size());
		Page<ProductEO> page = productService.page(new Page<>(1, 3), new QueryWrapper<ProductEO>().orderByAsc("price"));//对优惠价格进行分页
		m.addAttribute("page", page);
		m.addAttribute("hdlist2", hdlist2);
		m.addAttribute("hdlist2pages", hdlist2.size()/3+(hdlist2.size()%3 == 0?0:1));
		m.addAttribute("hslist2", hdlist2.size());
		Page<ProductEO> page2 = productService.page(new Page<>(1, 3), new QueryWrapper<ProductEO>().orderByDesc("price"));
		m.addAttribute("page2", page2);
		
		
		return "index";
	}
	
	
//	@GetMapping("/list/{id}/{current}/{sort}")
//	public String list(@PathVariable Integer id , @PathVariable Integer current, Integer sort, Model m ) {
//
//		List<String> status = new ArrayList<String>();
//		status.add("默认");
//		status.add("销量");
//		status.add("价格");
//
//
//		if(current <= 0){
//			current=1;
//		}
//
//
//		Map<String ,Object> data = Maps.newHashMap();
//		List<ProdtypeEO> allList = prodtypeService.list();
//		List<ProdtypeEO> parentList = new ArrayList<>();
//		for(ProdtypeEO eo:allList) {//查找所有的父类型
//			if(eo.getParentId() == null) {
//				List<ProdtypeEO> childs = new ArrayList<>();
//				for(ProdtypeEO eo1:allList) {//查找当前父类型的子类型
//					if(eo1.getParentId() == eo.getTypeId()) {
//						childs.add(eo1);
//					}
//				}
//				eo.setChilds(childs);
//				parentList.add(eo);
//			}
//		}
//
////		ProdtypeEO curProdTypeEO =  prodtypeService.getById(id);
//
					//		data.put("prodtypelist", parentList);
					//		data.put("newprod",  productService.newProd());
//
//		m.addAttribute("prodtypelist", parentList);
//		m.addAttribute("newprod",  productService.newProd());
//
//		int count = productService.count(new QueryWrapper<ProductEO>().eq("type_id", id));//总个数
//		int pages = 0;//总页数
//		if(count <=0) {
//			pages=0;
//		}else {
//			pages = count /24;
//			if(count % 24 !=0) {
//				pages++;
//			}
//		}
//		if(current > pages) {
//			current = pages;
//		}
//
//
//
//		Page<ProductEO> page = productService.page(new Page<>(current, 24),new QueryWrapper<ProductEO>().eq("type_id", id));//默认页面
//		Page<ProductEO> kucun = productService.page(new Page<>(current, 24),new QueryWrapper<ProductEO>().eq("type_id", id).orderByDesc("kucun"));//根据库存进行排序
//
//
//		m.addAttribute("datalist",page.getRecords());
//		m.addAttribute("page",page);
//		m.addAttribute("typeId",id);
//		m.addAttribute("kucun", kucun.getRecords());
//		m.addAttribute("kucun", kucun);
//		m.addAttribute("kucun",id);
//		m.addAttribute("status", status);
//		m.addAttribute("sort", sort);
//
//
//
//		return "list";
//	}





	@GetMapping("/list/{id}/{current}")
	public String list(@PathVariable Integer id, @PathVariable Integer current, Model m , @RequestParam(defaultValue ="prod_id") String orderby) {
		if (current <= 0)
		{
			current = 1;
		}

		Map<String, Object> data = Maps.newHashMap();

		List<ProdtypeEO> allList = prodtypeService.list();
		List<ProdtypeEO> parentList = new ArrayList<>();

		for (ProdtypeEO eo : allList)
		{// 查找所有父类
			if (eo.getParentId() == null)
			{
				List<ProdtypeEO> childs = new ArrayList<>();
				for (ProdtypeEO eo1 : allList)
				{// 查找所有的子类
					if (eo1.getParentId() == eo.getTypeId())
					{
						childs.add(eo1);
					}
				}
				eo.setChilds(childs);
				parentList.add(eo);
			}
		}

		ProdtypeEO curSysProdTypeEO = prodtypeService.getById(id);
		m.addAttribute("typeId", id);
		m.addAttribute("typeName", curSysProdTypeEO.getTypeName());

		data.put("prodtypelist", parentList);
		data.put("newprod", productService.newProd());

		m.addAttribute("prodtypelist", parentList);
		m.addAttribute("newprod", productService.newProd());

		int count = productService.count(new QueryWrapper<ProductEO>().eq("type_id", id));// 总个数

		int pages = 0;// 总页数
		if (count <= 0)
		{
			pages = 0;
		} else
		{
			pages = count / 24;
			if (count % 24 != 0)
			{
				pages++;
			}
		}
		if (current > pages)
		{
			current = pages;
		}
		QueryWrapper<ProductEO> qw = new QueryWrapper<ProductEO>().eq("type_id", id);
		qw.orderByDesc(orderby);
		Page<ProductEO> page = productService.page(new Page<>(current, 12), qw);
		m.addAttribute("page", page);
		m.addAttribute("datalist", page.getRecords());

		return "list";
	}









	@GetMapping("/test")
	public String test() {
	
		
		return "list";
	
	}
	
	
	
	

	@GetMapping({"","/detail/{prod}"})
	public String detail(@PathVariable Integer prod , Model m ) {
		
		List<ProdtypeEO> allList = prodtypeService.list();
		List<ProdtypeEO> parentList = new ArrayList<>();
		for(ProdtypeEO eo:allList) {//查找所有的父类型
			if(eo.getParentId() == null) {
				List<ProdtypeEO> childs = new ArrayList<>();
				for(ProdtypeEO eo1:allList) {//查找当前父类型的子类型
					if(eo1.getParentId() == eo.getTypeId()) {
						childs.add(eo1);
					}
				}
				eo.setChilds(childs);
				parentList.add(eo);
			}
		}
		ProductEO product = productService.getById(prod);
		
		m.addAttribute("prod", product);
		m.addAttribute("parentList", parentList);
		m.addAttribute("newprod", productService.newProd());
		
		
	
	return "detail";
}
	
}
